package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.adapter.SearchStaffAdapter;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.CodeListBean;
import com.csweimei.rentmanagementtreasure.http.entity.SearchStaffBean;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.pop.WarnPop;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class GiftCodeVM extends TitleVM {
    public GiftCodeVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("赠送席位码");
    }

    public ObservableField<String> yuser_id = new ObservableField<>();

    public ObservableField<CodeListBean.DataDTO> entity = new ObservableField<>();

    public BindingCommand gift = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            WarnPop warnPop = new WarnPop(mActivity, "温馨提示", "是否将席位码赠送与他?", new WarnPop.OnSelCallBack() {
                @Override
                public void onCallBack(String s) {
                    if (ValidateUtils.isEmptyString(yuser_id.get())){
                        ToastUtils.showShort("请选择赠送对象");
                        return;
                    }
                    giftCode();
                }
            });
            PopUtils.showPop(mActivity,warnPop,false);


        }
    });

    private void giftCode() {
        Map<String,String> map = new HashMap<>();
        map.put("user_id",model.getLoginInfo().getData().getUser_id());
        map.put("codeid",entity.get().getCodeID()+"");
        map.put("yuser_id",yuser_id.get());
        model.deliver_code(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult o) {
                        if (o.issucc){
                            ToastUtils.showShort(o.msg);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public ObservableList<SearchStaffItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<SearchStaffItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_search_staff);

    public void choose(String toString) {
        Map<String,String> map = new HashMap<>();
        map.put("phone",toString);
        model.seachbroker(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<SearchStaffBean>() {
                    @Override
                    public void onNext(SearchStaffBean o) {
                        if (o.issucc){

                            setAdapter(o.getData());
//                            observableList.clear();
//                            if (o.getData().size() > 0){
//                                for (SearchStaffBean.DataDTO bean : o.getData() ) {
//                                    SearchStaffItemVM itemVM = new SearchStaffItemVM(titleVM);
//                                    itemVM.setData(bean);
//                                    observableList.add(itemVM);
//                                }
//
//                            }

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public ObservableField<SearchStaffAdapter> adapter = new ObservableField<>(new SearchStaffAdapter());
    private void setAdapter(List<SearchStaffBean.DataDTO> data) {
        adapter.get().setList(data);
        adapter.get().addChildClickViewIds(R.id.sel);
        adapter.get().setOnItemChildClickListener((adapter1, view, position) -> {
            adapter.get().setSel(position);
            yuser_id.set(data.get(position).getUserid()+"");
        });

    }


}
